<?php
class Document extends AppModel {
   public $actsAs = array('Containable','Revision','SoftDelete');
   var $belongsTo = array(
		'User' => array(
			'className' => 'Usermgmt.User',
			'foreignKey' => 'user_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);
	
	function uploadToGoogle($id,$file){
		$data = $this->find('first',array(
			'conditions'=>array('Document.id'=>$id),
		));
		$xoauth_requestor_id = 'its@sbm-itb.ac.id';
		//$program = $visit['Thesis']['Program']['name'];
		$params = array(
			'xoauth_requestor_id' => $xoauth_requestor_id,
			'title'=>'ISO',
			'title-exact'=>'true'
		);
		
		$resourceId = 'folder:0B5Tm_OdcKvb1R1hfM1YyTU52c3M';
		$this->startUpload($xoauth_requestor_id,$resourceId,$data);
		//ClassRegistry::init('Gdata2.Doc')->preventDownload($params2,$resourceId);
/*
		
		$collections = ClassRegistry::init('Gdata2.Doc')->getCollectionList($params);
		//debug($collections);
		if(isset($collections['feed']['entry'])){//jika folder/collection dengan nama ISO ada
			$collection_list = $collections['feed']['entry'];
			$collection_lists = array();
			if(!isset($collection_list[0])){
				$collection_lists[0] = $collection_list;
			}else{
				$collection_lists = $collection_list;
			}
			//jaga-jaga kalau ada beberapa collection dengan nama yg sama
			foreach($collection_lists as $collection){
				//$resourceId = $collection['gd:resourceId'];
				$resourceId = 'folder:0B5Tm_OdcKvb1R1hfM1YyTU52c3M';
				//echo $resourceId;
				
				$this->startUpload($xoauth_requestor_id,$resourceId,$data);
				ClassRegistry::init('Gdata2.Doc')->preventDownload($params,$resourceId);
			}
			die();
		}else{//jika folder dengan nama $program belum ada
			//echo 'cek folder ISO -> belum ada';
			$create_params = array(
				'xoauth_requestor_id' => $xoauth_requestor_id
			);
			$result = ClassRegistry::init('Gdata2.Doc')->createCollection($create_params, 'ISO');
			if($result){
				//echo 'create folder ISO -> created';
				$newResourceId = $result['entry']['gd:resourceId'];
				$this->startUpload($xoauth_requestor_id,$newResourceId,$data);
				//ClassRegistry::init('Gdata2.Doc')->preventDownload($params,$newResourceId);
			}else{
				//echo 'create folder ISO -> failed';
			}
		}
*/
		return true;
	}
	
	function startUpload($xoauth_requestor_id,$resourceId,$data){
		$feed = 'https://docs.google.com/feeds/upload/create-session/default/private/full/'.$resourceId.'/contents';
		$path = WWW_ROOT.'files'.DS.'documents'.DS.$data['Document']['file'];
		$type = $data['Document']['mime'];
		//echo 'uploading '.$path;
		$params = array('xoauth_requestor_id' => $xoauth_requestor_id);
		$docs = ClassRegistry::init('Gdata2.Doc')->resumableUpload($feed,$params,$path,$type);
		if($docs){
			$newResourceId = $docs['entry']['gd:resourceId'];
			ClassRegistry::init('Gdata2.Doc')->preventDownload($params,$newResourceId);
			ClassRegistry::init('Document')->id = $data['Document']['id']; 
			ClassRegistry::init('Document')->saveField('gid',$newResourceId);
			
			ClassRegistry::init('Document')->id = $data['Document']['id']; 
			ClassRegistry::init('Document')->saveField('link',serialize($docs['entry']['link']));
			//echo $path.' -> uploaded';
		}else{
			//echo $path.' -> upload failed';
		}
	}
}
